package com.zzq.guli.service.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzq.guli.service.edu.entity.*;
import com.zzq.guli.service.edu.mapper.ChapterMapper;
import com.zzq.guli.service.edu.mapper.CourseDescriptionMapper;
import com.zzq.guli.service.edu.mapper.CourseMapper;
import com.zzq.guli.service.edu.mapper.VideoMapper;
import com.zzq.guli.service.edu.service.CourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.sql.Wrapper;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author ZZQ
 * @since 2024-04-12
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {

    @Autowired
    CourseMapper courseMapper;

    @Autowired
    CourseDescriptionMapper courseDescriptionMapper;


    @Autowired
    ChapterMapper chapterMapper;


    @Autowired
    VideoMapper videoMapper;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String saveCourseInfo(CourseInfoForm courseInfoForm) {

        //保存课程基本信息
        Course course = new Course();
        course.setStatus(Course.COURSE_DRAFT);
        BeanUtils.copyProperties(courseInfoForm, course);
        courseMapper.insert(course);

        //课程描述信息
        CourseDescription courseDescription = new CourseDescription();
        courseDescription.setDescription(courseInfoForm.getDescription());
        courseDescription.setId(courseInfoForm.getId());
        courseDescriptionMapper.insert(courseDescription);

        return course.getId();

    }

    @Override
    public Page<CourseVo> selectPage(Long page, Long limit, CourseQuery courseQuery) {

        QueryWrapper<CourseQuery> courseQueryQueryWrapper = new QueryWrapper<>();
        courseQueryQueryWrapper.orderByAsc("c.publish_time");

            if(!StringUtils.isEmpty(courseQuery.getSubjectParentId())){
              courseQueryQueryWrapper.eq("c.subject_parent_id",courseQuery.getSubjectParentId());
            }
            if(!StringUtils.isEmpty(courseQuery.getSubjectId())){
                courseQueryQueryWrapper.eq("c.subject_id",courseQuery.getSubjectId());
            }
            if(!StringUtils.isEmpty(courseQuery.getTitle())){
                courseQueryQueryWrapper.like("c.title",courseQuery.getTitle());
            }
            if(!StringUtils.isEmpty(courseQuery.getTeacherId())){
                courseQueryQueryWrapper.eq("c.teacher_id",courseQuery.getTeacherId());
            }

        Page<CourseVo> courseVoPage=new Page(page, limit);
        List<CourseVo> courseVoList=courseMapper.selectPageByQuery(courseVoPage,courseQueryQueryWrapper);
        courseVoPage.setRecords(courseVoList);
        return courseVoPage;
    }

    @Override
    public void updateCourseInfo(CourseInfoForm courseInfoForm) {
        //保存课程基本信息
        Course course = new Course();
        course.setStatus(Course.COURSE_DRAFT);
        BeanUtils.copyProperties(courseInfoForm, course);
        courseMapper.updateById(course);

        //课程描述信息
        CourseDescription courseDescription = new CourseDescription();
        courseDescription.setDescription(courseInfoForm.getDescription());
        courseDescription.setId(courseInfoForm.getId());
        courseDescriptionMapper.updateById(courseDescription);

    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void removeCourseById(String courseId) {
        //删除课程基本信息
        courseMapper.deleteById(courseId);
        //删除课程描述信息
        courseDescriptionMapper.deleteById(courseId);
        //删除章节信息
        QueryWrapper<Chapter> chapterQueryWrapper = new QueryWrapper<>();
        chapterQueryWrapper.eq("course_id",courseId);
        chapterMapper.delete(chapterQueryWrapper);
        //删除视频信息
        QueryWrapper<Video> videoQueryWrapper = new QueryWrapper<>();
        videoQueryWrapper.eq("course_id",courseId);
        videoMapper.delete(videoQueryWrapper);

    }

    @Override
    public boolean publishCourseById(String id) {
        Course course = new Course();
        course.setId(id);
        course.setPublishTime(new Date());
        course.setStatus(Course.COURSE_NORMAL);
        return this.updateById(course);
    }

    @Override
    public CoursePublishVo getCoursePublishVoById(String id) {
        return baseMapper.selectCoursePublishVoById(id);
    }

    @Override
    public List<Course> getCourseList(WebCourseQuery webCourseQuery) {
        QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
        //查询已发布的课程
        queryWrapper.eq("status", Course.COURSE_NORMAL);

        String subjectParentId = webCourseQuery.getSubjectParentId();//一级
        String subjectId = webCourseQuery.getSubjectId();//二级
        String buyCountSort = webCourseQuery.getBuyCountSort();//销量排序
        String publishTimeSort = webCourseQuery.getPublishTimeSort();//发布时间排序
        String priceSort = webCourseQuery.getPriceSort();//价格排序

        if(!StringUtils.isEmpty(subjectParentId)){
            queryWrapper.eq("subject_parent_id", subjectParentId);
        }

        if(!StringUtils.isEmpty(subjectId)){
            queryWrapper.eq("subject_id", subjectId);
        }

        if(!StringUtils.isEmpty(buyCountSort)){
            queryWrapper.orderByDesc("buy_count");
        }
        if(!StringUtils.isEmpty(publishTimeSort)){
            queryWrapper.orderByDesc("publish_time");
        }
        if(!StringUtils.isEmpty(priceSort)){
            if("1".equals(priceSort)){
                queryWrapper.orderByAsc("price");
            }else{
                queryWrapper.orderByDesc("price");
            }
        }

        return baseMapper.selectList(queryWrapper);

    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public WebCourseVo selectWebCourseVoById(String id) {
        //浏览数+1
        //        Course course = baseMapper.selectById(id);
        //        course.setViewCount(course.getViewCount() + 1);
        //        baseMapper.updateById(course);
        baseMapper.updateViewCountById(id);
        //获取课程信息
        return baseMapper.selectWebCourseVoById(id);
    }
}
